package com.lantu.wisdom.common.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Random;

/**
 * @Description: 编码生成工具类
 * @Author: duhui
 * @Date: 2020/1/7 16:08
 */
//TODO duhui 待确定编码规则
public class CreateCodeUtil {


    public static String createUserCode() {
        return "USE" + createBaseCode();
    }

    public static String createCircleCode() {
        return "CIR" + createBaseCode();

    }
    public static String createBookCode() {
        return "BOOK" + createBaseCode();

    }
    public static String createBookTypeCode() {
        return "BTYPE" + createBaseCode();

    }

    public static String createActivityCode() {
        return "ACT" + createBaseCode();

    }

    public static String createOrderCode() {
        return "JDZX" + createBaseCode();

    }

    /**
     * 书架编码
     * @return
     */
    public static String createBookShelfCode(){
        return "SHELF"+createBaseCode();
    }

    /**
     * 问候诗句编码
     * @return
     */
    public static String createVerseCode(){
        return  "VERSE"+createBaseCode();
    }

    /**
     * 临时图片编码
     * @return
     */
    public static String createPictureCode(){
        return  "PICTURE"+createBaseCode();
    }

    private static String createBaseCode(){
        return System.currentTimeMillis()+buileValidateCode(4);
    }
    /**
     * 随机获取一个小写字母
     *
     * @param
     */
    public static char getLetter() {
        char c = (char) getRadomInt(97, 122);
        return c;
    }

    /**
     * 随机获取一个大写字母
     *
     * @param
     */
    public static char getUpper() {
        char c = (char) getRadomInt(65, 90);
        return c;
    }

    /**
     * 随机获取一个0-9的数字
     *
     * @return
     */
    public static int getNum() {
        return getRadomInt(0, 9);
    }

    /**
     * 获取一个范围内的随机数字
     *
     * @return
     */
    public static int getRadomInt(int min, int max) {
        Random random = new Random();
        return random.nextInt(max - min + 1) + min;
    }

    /**
     * 自定义生成账号
     *
     * @return
     */
    public static String getAccountNumber() {
        StringBuffer sb = new StringBuffer();
        for (int j = 1; j <= 8; j++) {
            if (j <= 3) {//前三位获取字母
                sb.append(getLetter());
            } else {//后两位用数字
                sb.append(getNum());
            }
        }
        String userName = sb.toString();
        System.out.println("账号" + userName);
        return userName;
    }

    /**
     * 获取密码
     *
     * @return
     */
    public static String getPassword() {
        return "e10adc3949ba59a";
    }

    /**
     * 密码加密
     *
     * @param password
     * @return
     */
    public static String getPasswordMd(String password) {
        return getPasswordMd5(password, 15);
    }

    /**
     * 密码加密
     *
     * @param password
     * @param length
     * @return
     */
    public static String getPasswordMd5(String password, int length) {
        try {
            MessageDigest messageDigest = MessageDigest.getInstance("MD5");
            messageDigest.update(password.getBytes());//此处传入要加密的byte类型值
            byte[] digest = messageDigest.digest();//此处得到的是md5加密后的byte类型值
            int i;
            StringBuilder sb = new StringBuilder();
            for (int offset = 0; offset < digest.length; offset++) {
                i = digest[offset];
                if (i < 0) {
                    i += 256;
                }
                if (i < 16) {
                    sb.append(0);
                }
                sb.append(Integer.toHexString(i));//通过Integer.toHexString方法把值变为16进制
            }
            return sb.toString().substring(0, length);//从下标0开始，length目的是截取多少长度的值

        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return null;
        }

    }


    /**
     * 生成指定位数的随机数编码
     * @param several   生成验证码的位数  默认4位
     * @return
     */
    public static String buileValidateCode(int several){
        int code= (int) ((Math.random()*9+1)*Math.pow(10,several-1));
        return code+"";
    }

    public static void main(String[] arg) {
        //e10adc3949ba59a 密码 123456 加密
//        System.out.println(genRandomPwd(15));
        System.out.println(CreateCodeUtil.createUserCode());
    }

}
